package com.jp.java8;

import java.util.Optional;

public class OptionalUsage {

    public static void main(String[] args) {
        Optional<Insurance> optionalInsurance =Optional.empty();
        Optional<Insurance> optionalInsurance1 = Optional.of(new Insurance());


        optionalInsurance1.orElseGet(Insurance::new);
        optionalInsurance1.orElse(new Insurance());
        optionalInsurance1.orElseThrow(RuntimeException::new);
        optionalInsurance1.orElseThrow(()->new RuntimeException("runtime exception"));


        Insurance insurance = optionalInsurance1.filter(i -> i.getName() == null).get();


        Optional<String> s = optionalInsurance1.map(i -> i.getName());
        s.ifPresent(System.out::println);
    }


    private static String getInsuranceNameByOptional(Insurance insurance){
        return Optional.ofNullable(insurance).map(i->i.getName()).orElse("unkown");
    }

    private static String getInsuranceNameByOptional1(Person person){
        return Optional.ofNullable(person)
                .map(i->i.getCar())
                .map(i->i.getInsurance())
                .map(i->i.getName())
                .orElse("unkown");
    }
}
